package ui;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
import javax.swing.border.Border;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import control.Constants;
import control.RabidController;


public class RABIDUI implements ActionListener, ListSelectionListener {

	private JTextPane displayArea;
	private JTextField queryArea;
	private RabidController controller;
	private JComboBox commandSelect;
	private JScrollPane resultsArea;
	private JList resultList;
	
	public RABIDUI(RabidController cont){
		
		controller = cont;
		JFrame frame = createPane();
		JPanel panel = createPanel();
		addImages(panel);
		frame.getContentPane().add(panel);
		addContent(panel);
		
		frame.setVisible(true);
	}
	
	private JFrame createPane(){
		JFrame myFrame = new JFrame("RABID");
		myFrame.setSize(600, 300);
		myFrame.setResizable(false);
		myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		return myFrame;
	}
	
	private JPanel createPanel(){
		JPanel myPanel = new JPanel();
		myPanel.setLayout(null);
		myPanel.setBackground(Color.WHITE);
		myPanel.setVisible(true);

		return myPanel;
	}
	
	//Lots of magic numbers
	private void addImages(JPanel panel) {
		try {
			Image raccoon = javax.imageio.ImageIO.read(
					Thread.currentThread().getContextClassLoader().getResource("bin/raccoon.png"));
			JLabel raccoonLabel = new JLabel(new ImageIcon(raccoon));
			raccoonLabel.setLocation(460, 150);
			raccoonLabel.setSize(90, 77);
			panel.add(raccoonLabel);
			
			Image paw = javax.imageio.ImageIO.read(
					Thread.currentThread().getContextClassLoader().getResource("bin/paw.png"));
			JLabel leftPaw = new JLabel(new ImageIcon(paw));
			JLabel rightPaw = new JLabel(new ImageIcon(paw));
			
			leftPaw.setLocation(505, 235);
			rightPaw.setLocation(545, 245);
			leftPaw.setSize(24, 24);
			rightPaw.setSize(24, 24);
			panel.add(rightPaw);
			panel.add(leftPaw);
			
		} catch (Exception e) {
			System.out.println("No image " + e);
		}		
	}
	
	private void addContent(JPanel panel){
		
		//Color for borders. Change this color to what we want it to be
		Color borderColor = new Color(204, 204, 255);
		Border lineBorder = BorderFactory.createLineBorder(borderColor, 2);
		
		queryArea = new JTextField();
		queryArea.setSize(400, 25);
		queryArea.setLocation(20,20);
		queryArea.setBorder(lineBorder);
		panel.add(queryArea);
		
		String[] commandList = {Constants.C_QUERY, Constants.C_BROWSE, 
				Constants.C_SIMILARTO, Constants.C_DISPLAY};
		commandSelect = new JComboBox(commandList);
		commandSelect.setSelectedIndex(0);
		commandSelect.addActionListener(this);
		commandSelect.setLocation(430, 20);
		commandSelect.setSize(100,25);
		commandSelect.setBackground(Color.white);
		panel.add(commandSelect);
		
		JButton goButton = new JButton("Go");
		goButton.setLocation(540, 20);
		goButton.setSize(50, 25);
		goButton.setBackground(borderColor);
		goButton.addActionListener(this);
		panel.add(goButton);
		
		JButton statsButton = new JButton(Constants.C_STATS);
		statsButton.setLocation(430,55);
		statsButton.setSize(100, 25);
		statsButton.setBackground(borderColor);
		statsButton.addActionListener(this);
		panel.add(statsButton);

		JButton indexButton = new JButton(Constants.C_INDEX);
		indexButton.setLocation(430,80);
		indexButton.setSize(100, 25);
		indexButton.setBackground(borderColor);
		indexButton.addActionListener(this);
		panel.add(indexButton);
		
		displayArea = new JTextPane();
		displayArea.setSize(250, 200);
		displayArea.setLocation(170, 55);
		displayArea.disable();
		displayArea.setBorder(lineBorder);
		panel.add(displayArea);
		
		resultList = new JList();
		resultList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		resultList.addListSelectionListener(this);
		
		resultsArea = new JScrollPane();
		resultsArea.setLocation(20, 55);
		resultsArea.setSize(140 ,200);
		resultsArea.setBorder(lineBorder);
		resultsArea.getViewport().setView(resultList);
		panel.add(resultsArea);
	}
	
	public void setDisplayContent(String text) {
		displayArea.setText(text);
	}
	
	public String[] getQueryString() {
	
		String[] terms = new String[0];
		if(queryArea.getText().length() != 0){
			terms = queryArea.getText().split(" ");	
		}
		return terms;
	}

	public void setResultPane(String[] files) {
		String filename;
		int lastSlash;
		
		for(int i = 0; i < files.length; i++) {
			lastSlash = files[i].lastIndexOf('\\') + 1;
			filename = files[i].substring(lastSlash);
			files[i] = filename;
		}
		resultList.setListData(files);
	}// end setResultsPane
	
	public String getSelectedFile() {
		if(resultList.getSelectedValue() != null) {
			return resultList.getSelectedValue().toString();
		} else {
			inform("Please select a file", false);
			return null;
		}
	}

	/**
	 * This function will display an error message to the user.
	 * The program should terminate after the message has been 
	 * displayed.
	 * 
	 * @param errorMessage the error message to display to the user
	 */
	public void inform(String errorMessage, boolean exit) {
		JOptionPane.showMessageDialog(null, errorMessage, "System Error", JOptionPane.ERROR_MESSAGE);
		
		if(exit)
			System.exit(-1);
	}//end Inform
	
	
	public void actionPerformed(ActionEvent e) {
		
		if(e.getActionCommand().equals("Go")) {
			controller.fetchCommand(commandSelect.getSelectedItem().toString());
		} else {
			controller.fetchCommand(e.getActionCommand());
		}
	}

	public void valueChanged(ListSelectionEvent e) {
		if(resultList.getSelectedValue() != null ) {
			queryArea.setText(resultList.getSelectedValue().toString());			
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new RABIDUI(new RabidController());
	}

}
